﻿#include<iostream>
#include<string>
#include<vector>
using namespace std;
void getlaststr(string& s, vector<int>ln, vector<int>rn, vector<int>kn) {
    string s1 = s;
    int n = s.length();
    int Q = ln.size();
    int xx = n;
    for (int i = 0; i < Q; i++) {
        string sub = s1.substr(ln[i] - 1, rn[i] - 1);
        if (kn[i] == 0) {
            s.insert(0, sub);
            xx = xx + rn[i] - ln[i]+1;
        }

        else if (kn[i] == 1)
            s.insert(xx, sub);
    }
}
int main(void) {
    int n, Q;
    cin >> n >> Q;
    string s;
    cin >> s;
    int i = 0, a;
    vector<int> lnums, rnums, knums;
    lnums.resize(Q); rnums.resize(Q); knums.resize(Q);
    while (cin >> a) {
        lnums[i] = a;
        i++;
        if (i >= Q)
            break;
    }
    i = 0;
    while (cin >> a) {
        rnums[i] = a;
        i++;
        if (i >= Q)
            break;
    }
    i = 0;
    while (cin >> a) {
        knums[i] = a;
        i++;
        if (i >= Q)
            break;
    }
    getlaststr(s, lnums, rnums, knums);
    bool vv = (s == "abcXabcdeZXabcdeZ");
    cout << s<<" "<<vv;

}